<template>
    <div>
        <Row class="formRow">
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle">菜单名称</span>
          </Col>
          <Col span="2" class="leftRow">
            <Input v-model="formInline.menuName" placeholder="菜单名称" clearable style="width: 100%" size="large"></Input>
          </Col>
          <Col span="2" class="rightRow">
            <span class="ModelInputTitle" style="width: 100%;text-align: right">父级菜单名称</span>
          </Col>
          <Col span="2" class="leftRow">
            <Input v-model="formInline.pidMenuName" placeholder="父级菜单名称" clearable style="width: 100%" size="large"></Input>
          </Col>
          <Col span="2" class="rightRow">
          <span class="ModelInputTitle">是否有效</span>
          </Col>
          <Col span="2" class="leftRow">
            <Select  size="large" style="width:100%" v-model="formInline.valid">
              <Option  value="1" >是</Option>
              <Option  value="0" >否</Option>
            </Select>
          </Col>
          <Col span="12" class="leftRow">
            <Button type="primary" @click="onSubmit">查询</Button>
            <Button type="primary" @click="resetForm">重置</Button>
          </Col>
        </Row>


      <Table :data="tableData" :columns="cloums" stripe :loading="loading"  style="margin-top: 20px"></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page :total="total" :current="1" :placement="placement" @on-change="changePage" show-total show-elevator show-sizer @on-page-size-change="changeSize"></Page>
        </div>
      </div>


      <Modal
        v-model="edit"
        title="编辑"
        @on-ok="editok"
        @on-cancel="cancel('您取消了操作')" width="600">
        <Row>
          <Col span="12">   <span class="ModelInputTitle">菜单名称</span><Input v-model="editForm.menuName" style="width: 200px"></Input></Col>
          <Col span="12">   <span class="ModelInputTitle">父级菜单名称</span><Input v-model="editForm.pidMenuName" style="width: 200px"></Input></Col>
        </Row>

        <Row style="margin-top: 20px">
          <Col span="12">   <span class="ModelInputTitle">菜单图标</span><Input v-model="editForm.icon" style="width: 200px"></Input></Col>
          <Col span="12">   <span class="ModelInputTitle">是否有效</span> <Select   v-model="editForm.valid" style="width: 200px">
          <Option  value="1" >是</Option>
          <Option  value="0" >否</Option>
        </Select></Col>
        </Row>
        <Row style="margin-top: 20px">
          <Col span="24">   <span class="ModelInputTitle">备注</span><Input v-model="editForm.remark" style="width: 200px"></Input></Col>
        </Row>
      </Modal>
    </div>
</template>
<script type="text/ecmascript-6">
  import axios from  "axios"
    export default {
        data() {
            return {
              formInline:{
                menuName:"",
                pidMenuName:"",
                valid:""
              },
              edit:false,
              editForm:{
                menuName:"",
                pidMenuName:"",
                valid:"",
                icon:"",
                remark:"",
                id:""
              },
              loading:true,
              page:1,
              pageSize:10,
              placement:"bottom",
              total:0,
              tableData:[],
              cloums:[
                {
                  title: '菜单名称',
                  key: 'menuName',
                  width:200,
                  align:"center"
                },
                {
                  title: '父级菜单名称',
                  key: 'pidMenuName',
                  width:200,
                  align:"center"
                },
                {
                  title: '菜单图标',
                  key: 'icon',
                  width:200,
                  align:"center"
                },
                {
                  title: '是否有效',
                  key: 'valid',
                  width:200,
                  align:"center"
                },
                {
                  title: '创建人',
                  key: 'creator',
                  width:200,
                  align:"center"
                },
                {
                  title: '更新人',
                  key: 'updater',
                  width:200,
                  align:"center"
                },
                {
                  title: '备注',
                  key: 'remark',
//                  width:200,
                  align:"center"
                },
                {
                  title: '操作',
                  width:200,
                  align:"center",
                  fixed:"right",
                  render:(h,params)=>{
                    return h("div",[
                      h("Button",{
                        props:{
                          type:"primary"
                        },
                        on:{
                          click:()=>{
                              this.edit = true;
                              this.editForm.menuName = params.row.menuName;
                              this.editForm.pidMenuName = params.row.pidMenuName;
                              this.editForm.valid = params.row.valid=="是"?"1":"0";
                              this.editForm.icon = params.row.icon;
                              this.editForm.remark = params.row.remark;
                              this.editForm.id = params.row.id;
                          }
                        }
                      },"编辑")
                    ])
                  }
                }
              ]
            }
        },
      mounted(){
          this.init();
      },
      methods:{
        init(page,pageSize,menuName,pidMenuName,valid){
          var qs = require("qs");
          var _this = this;
          var param = qs.stringify({
            page:page?page:this.page,
            pageSize:pageSize?pageSize:this.pageSize,
            menuName:menuName?menuName:"",
            pidMenuName:pidMenuName?pidMenuName:"",
            valid:valid?valid:""
          });
          axios.defaults.headers.common['Authorization'] = sessionStorage.getItem("token");
          axios.post("/menu/querySysMenuListByPage",param).then((res)=>{
            if(res.data.code=="000000"){
              _this.loading = false;
              _this.total = res.data.count;
              for(let i in res.data.data){
                res.data.data[i].valid=res.data.data[i].valid =="1"?"是":"否"
              }
              _this.tableData = res.data.data;
            }else{
              _this.$Notice.error({title:res.data.msg});        }
          })
        },
        onSubmit(){
          this.init(this.page,this.pageSize,this.formInline.menuName,this.formInline.pidMenuName,this.formInline.valid);
        },
        resetForm(){
          this.formInline.menuName = "";
          this.formInline.pidMenuName = "";
          this.formInline.valid = "";
        },
        changePage(page){
          this.page = page;
          this.loading = true;
          this.init();
        },
        changeSize(size){
          this.placement = size>=20?"top":"bottom";
          this.pageSize = size;
          this.loading = true;
          this.init();
        },
        editok(){
          var qs = require("qs");
          var _this = this;
          var param = qs.stringify({
            menuName:this.editForm.menuName,
            remark:this.editForm.remark,
            icon:this.editForm.icon,
            pidMenuName:this.editForm.pidMenuName,
            valid:this.editForm.valid,
            id:this.editForm.id
          })
          axios.post("/menu/updateMenu",param).then((res)=>{
            if(res.data.code=="000000"){
              this.$Notice.success({title:"修改成功"});
              this.init();
            }else{
              this.$Notice.error({title:res.data.msg});
            }
          })
        },
        cancel (msg) {
          this.$Notice.info({title:msg});

        }
      }
    }
</script>
